package com.qianbao.finance.sync.modules.elasticsearch.websocket.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qianbao.finance.sync.common.utils.SpringContextUtils;
import com.qianbao.finance.sync.modules.elasticsearch.websocket.server.ShareConfig;



/**
 * ZabbixDataUtil 配置Zabbix Api地址 获取监控指标
 * 
 * @author WangYuan
 * @date 2016年7月23日下午3:39:19
 */
public class ZabbixDataUtil {

    private ZabbixDataUtil() {}

    private static final String encoding = "utf-8";

    private static final String contentType = "application/json-rpc";
    
    private static String zabbixUrl = ((ShareConfig) SpringContextUtils.getBean("shareConfig")).getZabbixApiUrl();

    private static String contentAuth =
            "{\"jsonrpc\": \"2.0\",\"method\": \"user.login\",\"params\": {\"user\": \"Admin\",\"password\": \"zabbix\"},\"id\": 1,\"auth\": null}";
    
    private static String contentItems =
            "{\"jsonrpc\": \"2.0\",\"method\": \"item.get\",\"params\": {\"output\": \"extend\",\"host\": \"%s\"},\"auth\": \"%s\",\"id\": 1}";


    public static String getToken() throws Exception {
/*        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(zabbixUrl);
        postMethod.setRequestHeader("Content-Type", contentType);
        StringRequestEntity stringRequestEntity =
                new StringRequestEntity(contentAuth, contentType, encoding);
        postMethod.setRequestEntity(stringRequestEntity);
        String responseStr;
        int resCode = client.executeMethod(postMethod);
        if (resCode == HttpStatus.SC_OK) {
            InputStream input = postMethod.getResponseBodyAsStream();
            byte[] responseBody = convertStream2Str(input).getBytes();
            responseStr = new String(responseBody, encoding);
            JSONObject jsonObject = JSON.parseObject(responseStr);
            return jsonObject.getString("result");
        }*/
        return null;
    }


    public static String getItems(String host, String authId) throws Exception {
/*        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(zabbixUrl);
        postMethod.setRequestHeader("Content-Type", contentType);
        String content = String.format(contentItems, host, authId);
        StringRequestEntity stringRequestEntity =
                new StringRequestEntity(content, contentType, encoding);
        postMethod.setRequestEntity(stringRequestEntity);
        String responseStr;
        int resCode = client.executeMethod(postMethod);
        if (resCode == HttpStatus.SC_OK) {
            InputStream input = postMethod.getResponseBodyAsStream();
            byte[] responseBody = convertStream2Str(input).getBytes();
            responseStr = new String(responseBody, encoding);
            JSONObject jsonObject = JSON.parseObject(responseStr);
            JSONArray jsonItems = jsonObject.getJSONArray("result");
            JSONObject resultJson = new JSONObject();
            resultJson.put("response", "success");
            JSONArray dataJson = new JSONArray();
            resultJson.put("data", dataJson);
            for (int i = 0, j = jsonItems.size(); i < j; i++) {
                JSONObject itemEntry = jsonItems.getJSONObject(i);
                JSONObject jsonItemResult = new JSONObject();
                jsonItemResult.put("key", itemEntry.getString("key_"));
                jsonItemResult.put("delay", Integer.parseInt(itemEntry.getString("delay")));
                jsonItemResult.put("lastlogsize", 0);
                jsonItemResult.put("mtime", 0);
                dataJson.add(jsonItemResult);
            }
            return resultJson.toString();
        }*/
        return null;
    }

    /**
     * 将输入流转换成字符串
     * 
     * @param input
     * @return
     * @throws IOException
     */
    public static String convertStream2Str(InputStream input) throws IOException {
        String temp;
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(input));
        while ((temp = br.readLine()) != null) {
            sb.append(temp);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        try {
            String id = ZabbixDataUtil.getToken();
            String oks = ZabbixDataUtil.getItems("win10-hezm", id);
            System.out.println(oks);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
